package com.mew.chess.core.game.shbz;


import com.mew.chess.core.common.handler.ChessHandlerMapCmdType;
import com.mew.chess.core.common.handler.ChessPlayAnnotation;
import com.mew.chess.core.common.handler.ChessPlayHandler;
import com.mew.chess.core.game.shbz.game.ShbzGameManager;
import com.mew.chess.core.game.shbz.game.ShbzRoomManager;
import com.mew.chess.core.game.shbz.game.ShbzTableManager;
import com.mew.chess.core.game.shbz.game.common.FishUserBO;
import com.mew.chess.core.game.shbz.game.message.request.HitRequestMessage;
import com.mew.chess.core.game.shbz.service.ShbzService;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.LongMessage;
import com.mew.common.rest.bo.GameSession;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@ChessPlayAnnotation(ChessHandlerMapCmdType.ShbzBet)
@Service
@Slf4j
public class ShbzBetHandler implements ChessPlayHandler<HitRequestMessage> {

    @Autowired
    private ShbzService shbzService;


    @Override
    public void handle(ZebraCommand<HitRequestMessage> request, GameSession gameSession) {
        int roomId = gameSession.getRoomId();
        long userId = gameSession.getUserId();

        ShbzRoomManager manager = ShbzGameManager.getInstance().getController(roomId);
        HitRequestMessage message = request.getRequest();

        if (message.getBetId()<1||message.getBetId()>manager.getBets().length){
            request.sendFailureCode(ResponseCode.BetError.code());
            return;
        }

        int isDouble = message.getIsDouble();
        int bet = ShbzGameManager.getInstance().getController(roomId).getBets()[message.getBetId()-1]*(isDouble+1);
        ShbzTableManager table = manager.getTableByUserId(userId);
        FishUserBO fishUserBO = table.getOnlineUserMap().get(userId);
        if (fishUserBO.getGoldNumber()<bet){
            request.sendFailureCode(ResponseCode.GoldNo.code());
            return;
        }

        LongMessage longMessage = shbzService.bet(userId, roomId, request.getRequest());
        request.sendMessage(longMessage);
    }
}
